
#import "PushHelper.h"
#import <CRToast/CRToast.h>
#import "Macros.h"
#import "BaseNavigationController.h"
#import "AppDelegate.h"
#import <AudioToolbox/AudioToolbox.h>

@implementation PushHelper

+(void)handlePushMessageActiveWithUserInfo:(NSDictionary *)userInfo{
    LGFLog(@"%@",userInfo);
    
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.userInfo = userInfo;
//    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.soundName = @"sound.wav";
    localNotification.alertBody = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    localNotification.fireDate = [NSDate date];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    [CRToastManager setDefaultOptions:@{kCRToastNotificationTypeKey : @(CRToastTypeNavigationBar),
                                        kCRToastFontKey             : FONT(16),
                                        kCRToastTextColorKey        : [UIColor whiteColor],
                                        kCRToastBackgroundColorKey  : [UIColor blackColor],
                                        kCRToastAutorotateKey       : @(YES)}];
    
    [CRToastManager showNotificationWithOptions:[self optionsWithPushDict:userInfo]
                                 apperanceBlock:^(void) {
                                     
                                 }
                                completionBlock:^(void) {
                                    
                                }];
}

+(void)handlePushMessageBackgroundWithUserInfo:(NSDictionary *)userInfo{
    LGFLog(@"%@",userInfo);
    //音效文件路径
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"customer" ofType:@"wav"];
//    //这里是指你的音乐名字和文件类型
//    NSLog(@"path---%@",path);
//    //组装并播放音效
//    SystemSoundID soundID;
//    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
//    AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
//    AudioServicesPlaySystemSound(soundID);
    [self presentViewControllerWithUserInfo:userInfo];
}

+ (NSDictionary*)optionsWithPushDict:(NSDictionary *)dict{
//   lNOTIF_POST(receivePushMessageSuccessNoti, nil);
    NSString *projectName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]; //获取APP名称
    NSString *text = [[dict objectForKey:@"aps"] stringForKey:@"alert"];
    NSMutableDictionary *options = [@{kCRToastNotificationTypeKey               :  @(CRToastTypeNavigationBar),
                                      kCRToastNotificationPresentationTypeKey   :  @(CRToastPresentationTypeCover),
                                      kCRToastUnderStatusBarKey                 :  @(YES),
                                      kCRToastAnimationInDirectionKey           :  @(CRToastAnimationDirectionTop),
                                      kCRToastAnimationSpringDampingKey         :  @(CRToastAnimationTypeLinear),
                                      kCRToastTextKey                           :  [NSString stringWithFormat:@"【%@】",projectName],
                                      kCRToastTextAlignmentKey                  :  @(NSTextAlignmentLeft),
                                      kCRToastTextColorKey                      :  [UIColor whiteColor],
                                      kCRToastTimeIntervalKey                   :  @(2),
                                      kCRToastTextMaxNumberOfLinesKey           :  @(3),
                                      kCRToastSubtitleTextKey                   :  text,
                                      kCRToastSubtitleTextColorKey              :  [UIColor whiteColor],
                                      kCRToastSubtitleTextAlignmentKey          :  @(NSTextAlignmentLeft),
                                      kCRToastStatusBarStyleKey                 :  @(CRToastTypeNavigationBar),
                                      kCRToastNotificationPreferredPaddingKey   :  @(10)} mutableCopy];
    return [NSDictionary dictionaryWithDictionary:options];
}

#pragma mark - 消息跳转页面
+ (void)presentViewControllerWithUserInfo:(NSDictionary *)target {
    LGFLog(@"设置需要调整的界面");
    //未登录
//    LGFLog(@"u_id == %zd",[OMUserInforDefault readLoginStatus]);
//    if(![OMUserInforDefault readLoginStatus]){
////        OMLoginViewController *loginController = [[OMLoginViewController alloc]init];
////        BaseNavigationController *navi = [[BaseNavigationController alloc] initWithRootViewController:loginController];
////        [UIApplication sharedApplication].keyWindow.rootViewController = navi;
////        LGFLog(@"jg跳转登录");
//    }
//    //已登录
//    else{
////        OMMessageController *svc = [[OMMessageController alloc]init];
////        UIViewController *VC = [self topVersibleController];
////        if ([VC isKindOfClass:[OMMessageController class]]) {
////            [kNotificationCenter postNotificationName:@"inMessageVCReceivePushNotification" object:nil];
////        }else{
////            [VC.navigationController pushViewController:svc animated:YES];
////            LGFLog(@"jg跳转抢单");
////        }
//    }
}

+ (UIViewController *)topVersibleController {
    
    UIViewController *rootVC = AppDelegateInstance.window.rootViewController;
    UINavigationController *navController = (UINavigationController *)rootVC;
    return [[navController viewControllers] lastObject];
}
@end
